package com.peoit.honeycomb.controller;

import com.peoit.honeycomb.application.SecurityApplication;
import com.peoit.honeycomb.domain.security.Permission;
import com.peoit.honeycomb.domain.security.Person;
import com.peoit.honeycomb.domain.security.Role;
import com.peoit.honeycomb.dto.CommonResultJsonDto;
import com.peoit.honeycomb.dto.PageCommonResultDto;
import com.peoit.honeycomb.dto.security.PermissionDto;
import com.peoit.honeycomb.dto.security.PersonDto;
import com.peoit.honeycomb.dto.security.PersonPageDto;
import com.peoit.honeycomb.dto.security.RoleDto;
import com.peoit.honeycomb.utils.CipherUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 后台用户管理
 * Created by zhouqiu on 2015/10/17.
 */
@Controller
@RequestMapping("/admin/person")
public class PersonController extends HoneycombBaseController {
    @Autowired
    SecurityApplication securityApplication;

    @RequestMapping("/list/{offset}")
    public ModelAndView list(PersonPageDto dto,@PathVariable int offset) {
        dto.setOffset(offset);
        List<PersonDto> dtos = securityApplication.findPersonPageBy(dto);

        Long count = securityApplication.findPersonCount(dto);
        PageCommonResultDto pageCommonResultDto = new PageCommonResultDto();
        pageCommonResultDto.setResult(dtos);
        pageCommonResultDto.setPageSize(dto.getPageSize());
        pageCommonResultDto.setCount(count);
        pageCommonResultDto.setOffset(offset);

        return view("/jsp/system/person/personlist",pageCommonResultDto);
    }

    @RequestMapping("/toPersonAdd")
    public ModelAndView toPersonAdd() {
        List<RoleDto> roleDtos = securityApplication.findRoleDtoList();
        return view("/jsp/system/person/personadd",roleDtos);
    }

    @RequestMapping("/personAdd")
    public @ResponseBody
    CommonResultJsonDto personAdd(PersonDto dto,String [] roleIds) {
        CommonResultJsonDto resultJsonDto = CommonResultJsonDto.success("成功！！");
        try {

//            String s = CipherUtil.generatePassword(dto.getPassword());
//            dto.setPassword(s);
            boolean b = this.securityApplication.personCreateOrUpdate(dto, roleIds);
            if (b == false) {
                resultJsonDto = CommonResultJsonDto.fail("失败！！");
            }
            return resultJsonDto;
        }catch (Exception e) {
            return resultJsonDto.fail("信息不完全！！");
        }
    }

    @RequestMapping("/toPersonUpdate/{id}")
    public ModelAndView toPersonUpdate(@PathVariable String id,ModelMap modelMap) {

        Person person = securityApplication.find(Person.class, id);
        PersonDto personDto = new PersonDto();
        PersonDto personDto1 = personDto.toDto(person);
        List<RoleDto> roleDtos = securityApplication.findRoleDtoList();

        modelMap.addAttribute("personDto1", personDto1);
        return view("/jsp/system/person/personedit",roleDtos);
    }


    @RequestMapping("/deletePerson")
    public @ResponseBody CommonResultJsonDto deletePerson( String id) {
        CommonResultJsonDto resultJsonDto = CommonResultJsonDto.success("删除成功！");
        this.securityApplication.deleteBy(Person.class, id);
        return resultJsonDto;
    }

}
